package com.work.chat.common.util;


import com.work.chat.common.eums.BizCodeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.UUID;

/**
 * @Copyright: Shanghai Smec Company.All rights reserved.
 * @Description:统一响应对象
 * @author:
 * @since: 2020/3/21 10:58 AM
 * @history: 1.2020/3/21 created by
 */

@Data
@NoArgsConstructor
public class Response implements Serializable {

    /**
     *  apass 成功必须要ok
     */
    private String code;

    /**
     * 状态码
     */
    private Integer statusCode;

    /**
     * 返回结果
     */
    private Object data;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 随机生成得请求id
     */
    private String requestId;

    public Response(String code, int statusCode, Object data, String message) {
        this.code = code;
        this.statusCode = statusCode;
        this.data = data;
        this.message = message;
        this.requestId = UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 成功
     */
    public static Response buildSuccess() {
        return new Response(StaticProperties.SUCCESS_OK, BizCodeEnum.SUCCESS.getCode(), null, BizCodeEnum.SUCCESS.getMessage());
    }

    /**
     * 成功，传入数据
     */
    public static Response buildSuccess(Object data) {
        return new Response(StaticProperties.SUCCESS_OK,BizCodeEnum.SUCCESS.getCode(), data, BizCodeEnum.SUCCESS.getMessage());
    }

    /**
     * 失败
     */
    public static Response buildError() {
        return new Response(StaticProperties.ERROR,BizCodeEnum.FAIL.getCode(), null, BizCodeEnum.FAIL.getMessage());
    }

    /**
     * 失败，自定义描述失败信息
     */
    public static Response buildError(String msg) {
        return new Response(StaticProperties.ERROR,BizCodeEnum.FAIL.getCode(), null, msg);
    }

    /**
     * 自定义状态码和错误信息
     */
    public static Response buildCodeAndMsg(int code, String massage) {
        return new Response(StaticProperties.ERROR,code, null, massage);
    }

    /**
     * 自定义状态码和错误信息
     */
    public static Response buildCodeAndMsg(int code, Object data, String massage) {
        return new Response(StaticProperties.ERROR,code, data, massage);
    }

    /**
     * 传入枚举，返回信息
     */
    public static Response buildResult(BizCodeEnum codeEnum) {
        return Response.buildCodeAndMsg(codeEnum.getCode(), codeEnum.getMessage());
    }

}